package com.example.win.android_banduyujia.login.view;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.example.win.android_banduyujia.R;
import com.example.win.android_banduyujia.entity.Constants;
import com.example.win.android_banduyujia.http.UrlContext;
import com.example.win.android_banduyujia.http.request.RegisterRequest;
import com.example.win.android_banduyujia.http.request.ResetPasswordRequest;
import com.example.win.android_banduyujia.http.request.VerifyCodeRequest;
import com.example.win.android_banduyujia.http.response.BaseHttpResponse;
import com.example.win.android_banduyujia.lib.util.StrUtils;
import com.example.win.android_banduyujia.peopleactivity.ui.activity.BaseTitleBarActivity;

/**
 * Created by Win on 2015/8/6.
 */
public class ForgetActivity extends BaseTitleBarActivity implements View.OnClickListener {

    private EditText  mEditPhone, mEditCode, mEditPassword;
    private Button    mBtnCode, mBtnSubmit;

    private String    mPhoneNo, mValidCode, mPwd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_back_password);

        InitTitles();
        InitViews();
    }

    private void InitTitles() {
        setTitle(R.string.title_text_view_forget_password);
        setRightButtonImageAndListener(R.mipmap.close, OnRightListener);
    }

    private View.OnClickListener OnRightListener = new View.OnClickListener(){

        @Override
        public void onClick(View v) {
            ForgetActivity.this.finish();
        }
    };

    private void InitViews() {
        mEditPhone = (EditText)findViewById(R.id.submit_phone);
        mEditCode = (EditText)findViewById(R.id.submit_valid_code);
        mEditPassword = (EditText)findViewById(R.id.submit_password);
        mBtnCode = (Button) findViewById(R.id.verification_code_button);
        mBtnSubmit = (Button) findViewById(R.id.activity_layout_button);

        mBtnCode.setOnClickListener(this);
        mBtnSubmit.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.verification_code_button: // 发送验证码
                mPhoneNo = mEditPhone.getText().toString();
                if(StrUtils.isEmpty(mPhoneNo)){
                    showToast(R.string.forget_password_activity_phone_not_null);
                    return;
                }
                sendValidCode(UrlContext.Url_VERIFYCODE.getContext(), mPhoneNo);
               // codebutton();
                break;
            case R.id.activity_layout_button: // 提交
                 submitUserEnter();
                break;
        }
    }

    private void codebutton() {
        if(mEditPhone !=null){
            return;
        }else{

            if(StrUtils.isEmpty(mPhoneNo)){
                showToast(R.string.forget_password_activity_phone_not_null);
                return;
            }
        }
    }

    private void submitUserEnter() {
         if(checkUserEnter()){
             ResetPasswordRequest request = new ResetPasswordRequest(mPhoneNo, mValidCode, mPwd);
             mAsycHttpRequest.doRequest(this, request, BaseHttpResponse.class, mResetPassHandler, true);
         }
    }

    public Handler mResetPassHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            showToast(R.string.forget_password_activity_toast_success);
            finish();
        }
    };

    private boolean checkUserEnter() {
        mPhoneNo = mEditPhone.getText().toString();
        mValidCode = mEditCode.getText().toString();
        mPwd = mEditPassword.getText().toString();
        if(StrUtils.isEmpty(mPhoneNo)){
            showToast(R.string.forget_password_activity_phone_not_null);
            return false;
        }
        if(StrUtils.isEmpty(mValidCode)){
            showToast(R.string.forget_password_activity_valid_code_not_null);
            return false;
        }
        if(mValidCode.length() < Constants.VALID_CODE_LENGTH){
            showToast(R.string.forget_password_activity_valid_code_not_null);
            return false;
        }
        if(mPwd.length() < Constants.PASSWORD_MIN_LENGTH){
            showToast(R.string.forget_password_activity_password_too_short);
            return false;
        }
        return  true;
    }
}
